﻿@page
@model FytSoa.Web.Pages.FytAdmin.Sys.MenuModel
@{
    ViewData["Title"] = "菜单管理";
    Layout = AdminLayout.Pjax(HttpContext);
}
<style>#tree{overflow:auto;}</style>
<div id="container">
    <div class="layui-col-220">
        <div class="layui-nav-title">菜单管理</div>
        <div id="tree" class="layui-tree-cus" style="overflow:auto;"></div>
    </div>
    <div class="right-col-body">
        <div class="list-wall">
            <div class="layui-form list-search">
                <button type="button" class="layui-btn layui-btn-sm" data-type="organizeAdd"><i class="layui-icon"></i> 新增</button>
                <button type="button" class="layui-btn layui-btn-sm" data-type="organizeDel"><i class="layui-icon"></i> 删除</button>
            </div>
            <table class="layui-hide" id="tablist" lay-filter="tool"></table>
        </div>
    </div>
    <script>layui.config({
            base: '/themes/js/modules/'
        }).use(['table', 'layer', 'jquery', 'common', 'tree'],
            function () {
                var table = layui.table,
                    layer = layui.layer,
                    $ = layui.jquery,
                    os = layui.common,
                    form = layui.form,
                    tree = layui.tree;
                $("#tree").css({ 'height': $(window).height() - 140 });
                table.render({
                    elem: '#tablist',
                    headers: os.getToken(),
                    url: '/api/menu/getpages',
                    cols: [
                        [
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'name', title: '菜单名称', width: 200, fixed: 'left' },
                            { field: 'nameCode', width: 150, title: '模块名' },
                            { field: 'urls', title: 'Url', width: 200 },
                            {
                                field: 'sort', width: 120, title: '排序', templet: function (res) {
                                    return '<a href="javascript:void(0)" class="table-sort text-color" lay-event="sortup" title="向上"><i class="layui-icon layui-icon-return"></i></a><a href="javascript:void(0)"  lay-event="sortdown" class="table-sort text-color" title="向下"><i class="layui-icon layui-icon-return"></i></a>';
                                }
                            },
                            { field: 'btnFunJson', title: '功能' },
                            { width: 100, title: '操作', templet: '#tool' }
                        ]
                    ],
                    height: $(window).height() - 150,
                    page: true,
                    limit: 1000,
                    id: 'tables'
                });

                var guid = '', typeName = '', active = {
                    initTree() {
                        os.ajax('api/menu/gettree/', {}, function (res) {
                            console.log(res);
                            tree.render({
                                elem: '#tree'
                                , data: res
                                , onlyIconControl: true 
                                , click: function (obj) {
                                    guid=obj.data.id;
                                    active.reload();
                                }
                            });
                        });
                    },
                    reload: function () {
                        table.reload('tables',
                            {
                                page: {
                                    curr: 1
                                },
                                where: {
                                    key: guid
                                }
                            });
                    },
                    organizeAdd: function () {
                        os.Open('添加菜单', '/fytadmin/sys/menumodify', '720px', '520px', function () {
                            active.reload();
                            var tree = $.fn.zTree.getZTreeObj("tree");
                            tree.reAsyncChildNodes(null, "refresh");
                        });
                    },
                    organizeDel: function () {
                        var checkStatus = table.checkStatus('tables')
                            , data = checkStatus.data;
                        if (data.length === 0) {
                            os.warning("请选择要删除的项目~");
                            return;
                        }
                        var str = '';
                        $.each(data, function (i, item) {
                            str += item.guid + ",";
                        });
                        layer.confirm('确定要执行批量删除吗？', function (index) {
                            layer.close(index);
                            var loadindex = layer.load(1, {
                                shade: [0.1, '#000']
                            });
                            os.ajax('api/menu/delete/', { parm: str }, function (res) {
                                layer.close(loadindex);
                                if (res.statusCode === 200) {
                                    active.reload();
                                    var tree = $.fn.zTree.getZTreeObj("tree");
                                    tree.reAsyncChildNodes(null, "refresh");
                                    os.success('删除成功！');
                                } else {
                                    os.error(res.message);
                                }
                            });
                        });

                    }
                };
                active.initTree();
                $('.list-search .layui-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                //监听工具条
                table.on('tool(tool)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') {
                        os.Open('编辑菜单', '/fytadmin/sys/menumodify/?guid=' + data.guid, '720px', '520px', function () {
                            var tree = $.fn.zTree.getZTreeObj("tree");
                            tree.reAsyncChildNodes(null, "refresh");
                            active.reload();
                        })
                    }
                    if (obj.event === 'sortdown') {
                        os.ajax('api/menu/sort', { p: data.parentGuid, i: data.guid, o: 1 }, function (res) {
                            if (res.statusCode === 200) {
                                active.reload();
                            }
                            else {
                                os.error(res.message);
                            }
                        });
                    }
                    if (obj.event === 'sortup') {
                        os.log(data);
                        os.ajax('api/menu/sort', { p: data.parentGuid, i: data.guid, o: 0 }, function (res) {
                            if (res.statusCode === 200) {
                                active.reload();
                            }
                            else {
                                os.error(res.message);
                            }
                        });
                    }
                });
            });</script>
    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon"></i> 修改</a>
    </script>
</div>

